package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.ao.PriceType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgAccountDetailMapper;
import com.ygqh.baby.po.YgAccountDetail;
import com.ygqh.baby.po.YgAccountDetailExample;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgAccountDetailService;
import com.ygqh.baby.service.YgUserBalanceService;
import com.ygqh.baby.service.YgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgAccountDetailServiceImpl implements YgAccountDetailService {

	@Autowired
	private YgAccountDetailMapper ygAccountDetailMapper;
	@Autowired
	private YgUserService userService;
	@Autowired
	private YgUserBalanceService ygUserBalanceService;

	@Override
	public List<YgAccountDetail> find() {
		YgAccountDetailExample example = new YgAccountDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAccountDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgAccountDetailExample example = new YgAccountDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAccountDetailMapper.countByExample(example);
	}

	@Override
	public YgAccountDetail findById(Long id) {
		return ygAccountDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgAccountDetail ygAccountDetail) {
		return ygAccountDetailMapper.insertSelective(ygAccountDetail);
	}

	@Override
	public int update(YgAccountDetail ygAccountDetail) {
		return ygAccountDetailMapper.updateByPrimaryKeySelective(ygAccountDetail);
	}

	@Override
	public ResultSet<YgAccountDetail> search(QueryInfo queryInfo, String q) {
		List<YgAccountDetail> list = ygAccountDetailMapper.selectSuper(queryInfo, q);
		int count = ygAccountDetailMapper.countSuper(q);
		ResultSet<YgAccountDetail> resultSet = new ResultSet<YgAccountDetail>(count, list);
		return resultSet;
	}

	@Override
	public ResultSet<YgAccountDetail> searchDetailByUser(QueryInfo queryInfo, String username, String source, Date startDate, Date endDate, String remark) {
		Long userId = null;
		YgUser user = userService.findByUserName(username);
		if (user != null) {
			userId = user.getId();
		}

		List<YgAccountDetail> list = ygAccountDetailMapper.searchDetailByUserId(queryInfo, username, userId, source, startDate, endDate, remark);
		Double total = ygAccountDetailMapper.calculateSumPrice(username, userId, source, startDate, endDate, remark);
		// Double total =
		// list.stream().mapToDouble(d->d.getPrice().doubleValue()).summaryStatistics().getSum();
		int count = ygAccountDetailMapper.countSearchDetailByUserId(userId, username, source, startDate, endDate, remark);

		ResultSet<YgAccountDetail> resultSet = new ResultSet<>(count, list);
		resultSet.setExt(total);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgAccountDetailExample example = new YgAccountDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgAccountDetail record = new YgAccountDetail();
		record.setStatus(DataStatus.Delete);
		return ygAccountDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgAccountDetail ygAccountDetail) {
		int r = 0;
		if (ygAccountDetail.getId() != null) {
			r = this.update(ygAccountDetail);
		} else {
			r = this.save(ygAccountDetail);
		}
		return r;
	}

	@Override
	public Message addForPaySuccess(Long userId, Long paymentCode, BigDecimal totalPrice, PayType payType, String remark) {
		YgAccountDetail accountDetail = this.findByPaymentCode(paymentCode);
		if (accountDetail == null) {
			YgUser user = userService.findById(userId);
			YgAccountDetail record = new YgAccountDetail();
			record.setUserId(userId);
			record.setPrice(totalPrice);
			record.setPaymentCode(paymentCode);
			record.setPayType(payType);
			record.setPriceType(PriceType.Income);
			record.setPaidTime(new Date());
			record.setCreateTime(new Date());
			record.setCreateBy(user.getUserName());
			record.setRemark("充值;" + remark);
			this.save(record);
		}
		return Message.success(null);
	}

	private YgAccountDetail findByPaymentCode(Long paymentCode) {
		YgAccountDetailExample example = new YgAccountDetailExample();
		example.createCriteria().andPaymentCodeEqualTo(paymentCode).andStatusEqualTo(DataStatus.Valid.name());
		List<YgAccountDetail> list = ygAccountDetailMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int add(Long orderId, Long userId, PriceType priceType, BigDecimal accountPrice, String operation) {
		YgAccountDetail record = new YgAccountDetail();
		record.setUserId(userId);
		record.setOrderId(orderId);
		record.setPriceType(priceType);
		record.setPrice(accountPrice);
		record.setRemark(operation);
		record.setCreateTime(new Date());
		return this.save(record);
	}

	@Override
	public YgAccountDetail findByOrderId(Long orderId, PriceType priceType) {
		YgAccountDetailExample example = new YgAccountDetailExample();
		example.createCriteria().andOrderIdEqualTo(orderId).andPriceTypeEqualTo(priceType.name()).andStatusEqualTo(DataStatus.Valid.name());
		List<YgAccountDetail> list = ygAccountDetailMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Message doBussnessForPaySuccess(Long paymentCode, Long userId, BigDecimal totalPrice, PayType payType, String remark) {
		YgAccountDetail accountDetail = this.findByPaymentCode(paymentCode);
		if (accountDetail == null) {
			int i = ygUserBalanceService.backAcountPrice(userId, totalPrice);
			if (i > 0) {
				this.addForPaySuccess(userId, paymentCode, totalPrice, payType, remark);
			}
		}
		return Message.success(null);
	}

	@Override
	public int addBatch(List<YgAccountDetail> accountDetail) {
		if (accountDetail != null && accountDetail.size() > 0) {
			return ygAccountDetailMapper.addBatch(accountDetail);
		}
		return 0;
	}

}
